package com.beauty.dressing.module.mine.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseActivity;
import com.beauty.dressing.common.bean.AddressManagerBean;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.common.interfaces.OnRecyclerViewItemListener;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.mine.adapter.AddressManageAdapter;
import com.beauty.dressing.module.mine.persenter.AddressManagerControl;
import com.beauty.dressing.module.mine.persenter.AddressManagerPresenter;
import com.beauty.dressing.rxandroid.rxbus.RxAction;
import com.beauty.dressing.rxandroid.rxbus.RxBus;
import com.beauty.dressing.rxandroid.rxbus.RxBusObserver;
import com.beauty.dressing.rxandroid.rxbus.RxConstants;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import io.reactivex.disposables.Disposable;


public class AddressManageActivity extends BaseActivity implements OnRecyclerViewItemListener {
    RecyclerView address_manage_rv;
    AddressManageAdapter addressManageAdapter;
    List<AddressManagerBean> mList;
    private Disposable mDisposable;
    User user;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_address_manage);
    }

    @Override
    public void findView() {
        TextView title_txt = (TextView) findViewById(R.id.title_txt);
        title_txt.setText("地址管理");
        findViewById(R.id.left_img_btn).setOnClickListener(this);
        findViewById(R.id.btn_new_address).setOnClickListener(this);
        address_manage_rv = findViewById(R.id.address_manage_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        address_manage_rv.setLayoutManager(linearLayoutManager);

        if (addressManageAdapter == null) {
            addressManageAdapter = new AddressManageAdapter(context);
        }
        address_manage_rv.setAdapter(addressManageAdapter);
        addressManageAdapter.setOnRecyclerViewItemListener(this);
        user = LBController.getInstance().getCacheManager().getLoginUser();
        registRxBus();
    }

    @Override
    public void initData() {
        presenter.getAllAddress(user.member_id, user.token);
//        presenter.getAllAddress(1, "3889f68da98eea80ffaf64ae8cae6ff4347a435c");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_img_btn:
                this.finish();
                break;
            case R.id.btn_new_address:
                startActivity(new Intent(context, NewAddressActivity.class));
                break;
        }
    }

    private AddressManagerPresenter presenter = new AddressManagerPresenter(new AddressManagerControl() {
        @Override
        public void showProgress() {

        }

        @Override
        public void onGetAllAddress(boolean isSuccess, int code, String message, List<AddressManagerBean> list) {
            mList = list;
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getStatus() == 1) {
                    Collections.swap(mList, 0, i);
                }
            }
            addressManageAdapter.setData(mList);
        }

    });

    @Override
    public void onItemViewOnClick(int type, int position) {
        switch (type) {
            case R.id.iv_edit_address:
                startActivity(new Intent(context, NewAddressActivity.class).putExtra("data", mList.get(position)));
                break;
            case 1:
                if (getIntent().getStringExtra("type") != null) {
                    setResult(666, new Intent().putExtra("AddressBean", mList.get(position)));
                    this.finish();
                }
                break;
        }
    }

    /**
     * 通过RxBus注册监听事件
     */
    private void registRxBus() {
        List<Integer> list = Arrays.asList(RxConstants.ACTION_UPDATEADDRESS_SUCCESS);
        RxBus.getDefault().toObservableWithCodes(list).subscribeWith(new RxBusObserver<RxAction>() {
            @Override
            protected void onRxSubscribe(Disposable d) {
                mDisposable = d;
            }

            @Override
            protected void onRxNext(RxAction value) {
                switch (value.code) {
                    case RxConstants.ACTION_UPDATEADDRESS_SUCCESS://地址更新
                        initData();
                        break;

                }
            }
        });
    }

    @Override
    public void onDestroy() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.onDestroy();
    }
}
